$(function () {
    var url = window.location.href;
    //var url2 = document.referrer;
    if (url.indexOf("192.168.3.67") > 0) {
        flushUserLogin("http://192.168.3.67/Advertiser_Mall/weijifen/home.html");
        /*  if(url2!="http://192.168.3.67/Advertiser_Mall/weijifen/home.html"){
         window.location.href = "http://192.168.3.67/Advertiser_Mall/weijifen/home.html"
         }*/
    } else if (url.indexOf("www.jifenguanggao.com") > 0) {
        flushUserLogin("http://www.jifenguanggao.com/Advertiser_Mall/weijifen/home.html");
        /* if(url2!="http://www.jifenguanggao.com/Advertiser_Mall/weijifen/home.html"){
         window.location.href = "http://www.jifenguanggao.com/Advertiser_Mall/weijifen/home.html"
         }*/
    } else {
        flushUserLogin("/Lvscore_User_M/userWeb/views/accounterCenter/index.html");
    }
    ajaxGetUserRemember();
    $("#doLogin").click(function () {
        var flag = $("#flag").val();
        var rememberMe = $("#rememberMe").val();
        var username = $("#name").val();
        var password = $("#password").val();
        if (isNotEmpty(username)) {
            if (isNotEmpty(password)) {
                $("#login_error").text("");
                ajaxDoLogin(flag, rememberMe, username, password);
            } else {
                $("#login_error").text("输入密码不能为空");
            }
        } else {
            $("#login_error").text("输入用户名不能为空");
        }
    });

});
function flushUserLogin(href) {
    var url = "/Lvscore_User_M/ajaxGetUser";
    $.post(url, {}, function (data) {
        if (isNotEmpty(data)) {
            ajaxFlushUnread(data);
            if (href) {
                window.location.href = href;
            }
        }
    });
}
//记住密码图片切换
function togglePic() {
    if ($(".remindPsd").attr("src") == "/Lvscore_User_M/userWeb/static/images/reminder_icon.png") {
        $(".remindPsd").attr("src", "/Lvscore_User_M/userWeb/static/images/contright_icon3.png");
        $("#rememberMe").attr("value", "");
    } else {
        $(".remindPsd").attr("src", "/Lvscore_User_M/userWeb/static/images/reminder_icon.png");
        $("#rememberMe").attr("value", "1");
    }
}
/**
 * 获取用户记住密码信息并通过json返回
 */
function ajaxGetUserRemember() {
    var cookie = new sweetCookie();
    var remember = cookie.getCookie("remember");
    if (isNotEmpty(remember)) {
        var url = "/Lvscore_User_M/ajaxGetUserRemember";
        $.post(url, {}, function (data) {
            if (isNotEmpty(data)) {
                $("#name").val(data.username);
                $("#password").val(data.password);
            } else {
                $("#name").val("");
                $("#password").val("");
            }
            $("#flag").val("1");
        }, "json");
    } else {
        $("#name").val("");
        $("#password").val("");
        $("#flag").val("1");
    }
}

/**
 * 用户登录
 * @param flag 用户是否修改了密码(修改了密码该值为空)
 * @param rememberMe 是否记住密码（不记住密码该值为空）
 * @param username 用户名
 * @param password 密码
 */
function ajaxDoLogin(flag, rememberMe, username, password) {
    loading("正在登录，请稍等...");
    var url = "/Lvscore_User_M/ajaxDoLogin";
    $.ajax({
        url:url,
        data:{
            flag: flag,
            rememberMe: rememberMe,
            username: username,
            password: password
        },
        type:"post",
        timeout:10000,
        success:function (data) {
            if (data.message == 1) {
                if (data.key) {
                    var cookie = new sweetCookie();
                    cookie.setCookie("remember", data.key, 1000 * 60 * 60 * 24 * 3);
                }
                var url = window.location.href;
                if (url.indexOf("192.168.3.67") > 0) {
                    window.location.href = "http://192.168.3.67/Advertiser_Mall/weijifen/home.html?token=" + data.token;
                } else if (url.indexOf("www.jifenguanggao.com") > 0) {
                    window.location.href = "http://www.jifenguanggao.com/Advertiser_Mall/weijifen/home.html?token=" + data.token;
                } else {
                    window.location.href = "/Lvscore_User_M/userWeb/views/accounterCenter/index.html";
                }
            } else if (data.message == -1) {
                $("#login_error").text("您输入的账号或密码有误，请重新输入");

            } else if (data.message == -2) {
                $("#login_error").text("您的账号已被冻结，请联系管理员");
            } else {
                $("#login_error").text("登录异常，请稍候重试");
            }
            //等待加载的效果消失
            $(".loadingBox").hide();
        },
        error:function(){
            $(".loadingBox").hide();
            alertBox("连接超时，请稍后重试","");
        },
        dataType:"json"
    });
}

function nextEnter(e) {
    var key = window.event ? e.keyCode : e.which;
    if (key == 13) {
        $("#password").focus().select();
    }
}

function loginEnter(e) {
    var key = window.event ? e.keyCode : e.which;
    if (key == 13) {
        $("#doLogin")[0].click();
    } else {
        $("#flag").val("");
    }
}